package com.dingding.order.core.publish.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.UserTaxiOrdert;
import com.dingding.order.core.publish.dao.IPublishTaxiOrderDao;

/**
 * @Description: 发布出租车订单dao
 * @author fengshuonan
 * @date 2016年7月1日11:37:39
 */
@Component("publishTaxiOrderDao")
public class PublishTaxiOrderDaoImpl extends BaseDaoImpl<UserTaxiOrdert> implements IPublishTaxiOrderDao {

	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * @Description: 判断当前用户是否有未完成的出租车订单
	 * @param userId 乘客id
	 * @author fengshuonan
	 * @return true 存在未完成的订单  false 不存在 
	 */
	@Override
	@Transactional(readOnly = true)
	public boolean haveUnfinishedTaxiOrder(String userId) {
		
		Criterion criterion = Restrictions.eq("placeOrderUserId", userId);
		
		List<UserTaxiOrdert> orders = null;
		
		try {
			orders = super.queryCriteriaList(criterion);
		} catch (Exception e) {
			log.error("查询未完成出租车订单出错！PublishTaxiOrderDaoImpl : haveUnfinishedTaxiOrder()", e);
		}
		
		if(orders == null || orders.size() <= 0){
			return false;
		}
		
		for(UserTaxiOrdert order : orders){
			
			Integer status = order.getOrderStatus();
			if(status != null && (status == 1 || status == 2 || status == 3)){
				return true;
			}
		}
		
		return false;
	}
	
}
